package com.bgh.dynobot;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Results extends Activity{
	
	private DynoDbAdapter DbHelper;
	String currentVehicle = null;
	int currentVehicleID = 0;
	boolean isMetric = true;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.results);   
        
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
 		SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
 		currentVehicle = appPreferences.getString("current_vehicle", "Default vehicle");
 		currentVehicleID = appPreferences.getInt("current_vehicle_id", 0);
 		
 		TextView tv = (TextView) findViewById(R.id.runVehicle);
		tv.setText(currentVehicle);
 		
 		Spinner spinner = (Spinner) this.findViewById(R.id.selectRun);
 		
        DbHelper = new DynoDbAdapter(this);
	    DbHelper.open();
	    
	    Cursor c = DbHelper.fetchAllRuns(currentVehicleID);
		
	    final ArrayList<String> vehicleList = new ArrayList<String>();

    	vehicleList.add("Last run");
    	if (c.moveToFirst()) {
    	   do{
    		   vehicleList.add(c.getString(c.getColumnIndex(DynoDbAdapter.KEY_RUN_DATE)));
    	   } while (c.moveToNext());
       	}	
       
       	final String[] items = (String[]) vehicleList.toArray(new String[0]);

       	ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                this, android.R.layout.simple_spinner_item, items);
	    
       	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       	spinner.setAdapter(adapter);       	
        c.close();
	    DbHelper.close();
	    
	    displayResults(currentVehicleID, (String)spinner.getSelectedItem());
    }
	
	public void displayResults(int vehicleID, String runDate){
		int runID = -1;
	    double time, distance, velocity = -1;
		
	    TableRow tr = null;
		TextView tv = null;
		TableLayout tl = null;
		boolean value60ft = false; 
		boolean value330ft = false;
		boolean value660ft = false;
		boolean value1000ft = false;
		boolean valueET = false;
		
		int lastVelocity = 0;
	    
		DbHelper = new DynoDbAdapter(this);
	    DbHelper.open();
	    Cursor c = null;
	    
	    c = DbHelper.fetchRunID(vehicleID, runDate);
	    if (c.moveToFirst()) {
	    	runID = (c.getInt(c.getColumnIndex(DynoDbAdapter.KEY_RUN_ID)));
	    }	
	    
	    tl = (TableLayout)findViewById(R.id.acceleration_table);
	    
	    DecimalFormat f = new DecimalFormat("0.00");
	    
	    c = DbHelper.fetchTimes(runID);
	    if (c.moveToFirst()) {
	    	do{
	    		time = (c.getDouble(c.getColumnIndex(DynoDbAdapter.KEY_RUN_TIME)));
	    		distance = (c.getDouble(c.getColumnIndex(DynoDbAdapter.KEY_DISTANCE)));
	    		velocity = (c.getDouble(c.getColumnIndex(DynoDbAdapter.KEY_VELOCITY)));
	    		
	    		if(isMetric){
	    			if(((velocity/1000) * 60 * 60) > lastVelocity + 20){
	    				tr = new TableRow(this);
	    				tv = new TextView(this);	    			
	    				tv.setText("0-" + (lastVelocity + 20) + "kmph");
	    				tv.setGravity(Gravity.LEFT);
	    				tv.setTextColor(Color.BLACK);
	    				tv.setTextSize(18);
	    				tr.addView(tv);
	    				
	    				tv = new TextView(this);	    			
	    				tv.setText(f.format(time) + "secs");
	    				tv.setGravity(Gravity.RIGHT);
	    				tv.setTextColor(Color.BLACK);
	    				tv.setTextSize(18);
	    				tr.addView(tv);
	    				
	    				tl.addView(tr);	    				
	    				lastVelocity += 20;
	    			}	    			
	    			
	    			//60ft
	    			if(distance > 18.288 && value60ft == false){
	    				tv = (TextView)findViewById(R.id.value60ft);
	    				tv.setText(f.format(time) + " seconds @ " + f.format((velocity/1000) * 60 * 60) + "kmph");
	    				value60ft = true;
	    			}
	    			
	    			//330ft
	    			if(distance > 100.584 && value330ft == false){
	    				tv = (TextView)findViewById(R.id.value330ft);
	    				tv.setText(f.format(time) + " seconds @ " + f.format((velocity/1000) * 60 * 60) + "kmph");
	    				value330ft = true;
	    			}
	    			
	    			//660ft
	    			if(distance > 201.168 && value660ft == false){
	    				tv = (TextView)findViewById(R.id.value660ft);
	    				tv.setText(f.format(time) + " seconds @ " + f.format((velocity/1000) * 60 * 60) + "kmph");
	    				value660ft = true;
	    			}
	    			
	    			//1000ft
	    			if(distance > 304.8 && value1000ft == false){
	    				tv = (TextView)findViewById(R.id.value1000ft);
	    				tv.setText(f.format(time) + " seconds @ " + f.format((velocity/1000) * 60 * 60) + "kmph");
	    				value1000ft = true;
	    			}
	    			
	    			//ET
	    			if(distance > 402.336 && valueET == false){
	    				tv = (TextView)findViewById(R.id.valueET);
	    				tv.setText(f.format(time) + " seconds @ " + f.format((velocity/1000) * 60 * 60) + "kmph");
	    				valueET = true;
	    			}
	    		}
	    		
	    	} while (c.moveToNext());
	    }	

	}

}
